package vip.xiaonuo.modular.business.util;

import cn.hutool.core.bean.BeanUtil;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.modular.manage.biindextype.entity.BiIndexType;
import vip.xiaonuo.modular.business.entity.BiUserIndexType.BiUserIndexType;
import vip.xiaonuo.modular.entitys.UserIndexTree;
import vip.xiaonuo.util.BIndexTypeHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 指标分类管理辅助类
 * @Author cy
 * @Date 2022/8/3 17:34
 * @Version 1.0
 */
public class UserIndexTypeHandler extends BIndexTypeHandler<UserIndexTree> {

    @Override
    public  List<UserIndexTree> allTree(List<BiIndexType> indexTypes) {
        UserIndexTree rootNode = getRootNode();
        return getChildren(rootNode,indexTypes);
    }

    @Override
    public UserIndexTree rootTree(List<BiIndexType> indexTypes) {
        UserIndexTree rootNode = getRootNode();
        rootNode.setChildren(getChildren(rootNode,indexTypes));
        return rootNode;
    }

    @Override
    public UserIndexTree getRootNode(){
        UserIndexTree tree = new UserIndexTree();
        tree.setTitle("指标分类管理");
        tree.setKey(0L);
        return tree;
    }

    @Override
    public  List<UserIndexTree> getChildren(UserIndexTree tree, List<BiIndexType> indexTypes){
        List<UserIndexTree> result = new ArrayList<>();
        for(BiIndexType indexType:indexTypes){
            if(isChild(tree,indexType)){
                UserIndexTree tree1 = new UserIndexTree();
                BeanUtil.copyProperties(indexType,tree1);
                tree1.setTitle(indexType.getName());
                tree1.setKey(indexType.getId());
                tree1.setChildren(getChildren(tree1,indexTypes));
                result.add(tree1);
            }
        }
        return result;
    }

    @Override
    public  boolean isChild(UserIndexTree tree,BiIndexType indexType){
        return tree.getKey().compareTo(indexType.getPid())==0;
    }

    public void setSelect(UserIndexTree tree,List<BiUserIndexType> biUserIndexTypes){
        List<UserIndexTree> list = tree.getChildren();
        for (UserIndexTree userIndexTree:list){
            for(BiUserIndexType type:biUserIndexTypes){
                if(isSelect(userIndexTree,type)){
                    userIndexTree.setIsSelect(CommonConstant.STATUS_Y);
                }
            }
            setSelect(userIndexTree,biUserIndexTypes);
        }
    }

    public  boolean isSelect(UserIndexTree tree,BiUserIndexType biUserIndexType){
        return tree.getKey().compareTo(biUserIndexType.getIndexTypeId())==0;
    }
}
